<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { usePagination } from "@/hooks/usePagination"
import { type FormInstance } from "element-plus"
import { Search, Refresh } from "@element-plus/icons-vue"
import { getconfigurationRuleApi, getxjzhApi } from "@/api/order/index"
import { useUserStore } from "@/store/modules/user"
import { formatDateTime } from "@/utils"
const userStore = useUserStore()
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const loading = ref<boolean>(false)
const tableDataA = ref<any[]>([])
//#region 查
const tableData = ref<any>([])
const searchFormRef = ref<FormInstance | null>(null)
// 获取所有下级
const getXJ = () => {
  getxjzhApi({
    page: 1,
    size: 10000
  }).then((res: any) => {
    tableDataA.value = res.data.list
    console.log(res.data.list, "获取所有下级")
  })
}
getXJ()
const ruleForm = reactive({
  xj: ""
})
//#endregion

// #region 查询搜索的下级订单
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  ruleForm.xj = ""
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
//#endregion
const searchId = ref<any>("")
// #region 查询所有下级
const getTableData = () => {
  if (userStore.userInfo.user_id == 1) {
    searchId.value = -1
  } else {
    searchId.value = userStore.userInfo.user_id
  }
  loading.value = true
  console.log(userStore.userInfo, "获取用户信息")
  getconfigurationRuleApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    userId: ruleForm.xj ? ruleForm.xj : searchId.value
  })
    .then((res: any) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
      console.log(res, "查询的数据")
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
//#endregion

// 查看详情
const dialogVisible = ref<boolean>(false)
const formData = ref<any>({})
const handleUpdate = (row: any) => {
  dialogVisible.value = true
  console.log(row)
  formData.value = row
}

/**加密 */
const toHideString = (val: any, len: any) => {
  if (val.length == 2) {
    return `${val.substring(0, len)}******${val.substring(val.length)}`
  } else {
    return `${val.substring(0, len)}******${val.substring(val.length - len)}`
  }
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="ruleForm">
        <el-form-item prop="team" label="下级账号">
          <el-select v-model="ruleForm.xj" clearable placeholder="请选择下级账号" style="width: 240px">
            <el-option v-for="item in tableDataA" :key="item.user_id" :label="item.user_name" :value="item.user_id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="table-wrapper">
        <el-table :data="tableData" v-horizontal-scroll="'always'">
          {{ tableData }}
          <!-- 表格多选框 -->
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column type="index" label="序号" width="120" align="center">
            <template #default="{ $index }">
              {{ (paginationData.currentPage - 1) * paginationData.pageSize + $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="user_name" label="下级账号" align="center" />
          <el-table-column prop="create" label="创建时间" align="center">
            <template #default="scope">
              {{ formatDateTime(scope.row.order_create) }}
            </template>
          </el-table-column>
          <el-table-column prop="order_id" label="订单编号" align="center" />
          <el-table-column prop="package_name" label="套餐" align="center" />
          <el-table-column prop="order_card_name" label="姓名" align="center" />
          <el-table-column prop="order_card_id" label="身份证号" align="center" />
          <el-table-column prop="order_consignee" label="收货人" align="center" />
          <el-table-column prop="order_take_mobile" label="收货电话" align="center">
            <template #default="scope">
              {{ toHideString(scope.row.order_take_mobile, 4) }}
            </template>
          </el-table-column>
          <el-table-column prop="order_take_address" label="收货地址" align="center" />
          <el-table-column prop="order_state" label="订单状态" align="center">
            <template #default="scope">
              <div
                :class="{
                  doing: scope.row.order_state <= 200 && scope.row.order_state > 199,
                  success: scope.row.order_state >= 100 && scope.row.order_state < 200,
                  error: scope.row.order_state >= 400,
                  again: scope.row.order_state < 0
                }"
              >
                {{ scope.row.order_state_msg }}
                <div class="text-ellipsis" v-if="scope.row.order_result" style="font-size: 12px">
                  <el-tooltip placement="top">
                    <template #content> {{ scope.row.order_result }} </template>
                    <span style="cursor: pointer">({{ scope.row.order_result }})</span>
                  </el-tooltip>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 新增/修改 -->
      <el-dialog v-model="dialogVisible" title="查看详情" width="80%">
        <el-form ref="formRef" :model="formData" label-width="100px" label-position="left">
          <el-form-item prop="user_name" label="下级账号">
            <el-input v-model="formData.user_name" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_id" label="订单编号">
            <el-input v-model="formData.order_id" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="package_name" label="套餐">
            <el-input v-model="formData.package_name" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_card_name" label="姓名">
            <el-input v-model="formData.order_card_name" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_card_id" label="身份证号">
            <el-input v-model="formData.order_card_id" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_consignee" label="收货人">
            <el-input v-model="formData.order_consignee" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_take_mobile" label="收货电话">
            <el-input v-model="formData.order_take_mobile" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_take_address" label="收货地址">
            <el-input v-model="formData.order_take_address" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item prop="order_state_msg" label="订单状态">
            <el-input v-model="formData.order_state_msg" placeholder="请输入" disabled />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
        </template>
      </el-dialog>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
// input 输入框长度
:deep(.el-input__wrapper) {
  width: 180px;
}
.doing {
  color: #b88230;
}
.error {
  color: red;
}
.success {
  color: #67c23a;
}
.again {
  color: #c45656;
}
</style>
